<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item size="mini" label="" prop="fnumber">
        <el-input
          v-model="queryParams.fnumber"
          placeholder="请输入分部分项编码"
          clearable
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini" label="" prop="fnameL2">
        <el-input
          v-model="queryParams.fnameL2"
          placeholder="请输入分部分项名称"
          clearable
          @input="handleQuery"
        />
      </el-form-item>
      <el-form-item size="mini">
        <el-button
          v-hasPermi="['system:post:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" size="small" :data="checkTypeList" height="calc(100vh - 150px)" @selection-change="handleSelectionChange">
      <el-table-column label="编码" align="center" prop="fnumber" min-width="70" />
      <el-table-column label="名称" align="center" prop="fnameL2" min-width="100" />
      <el-table-column label="专业类别" align="center" prop="fnameL2" min-width="70">
        <template slot-scope="scope">
          <span>{{ scope.row.cfspecialtypeid | specialType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" min-width="180">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            trigger="click"
          >
            <div class="ellipsis-all-content">{{ scope.row.fdescriptionL2 }}</div>
            <div slot="reference" class="ellipsis">{{ scope.row.fdescriptionL2 }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creatorName" min-width="90" />
      <el-table-column label="创建时间" align="center" prop="createTime" min-width="120">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            trigger="click"
          >
            <div class="ellipsis-all-content">{{ scope.row.fcreatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</div>
            <div slot="reference" class="ellipsis">{{ scope.row.fcreatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updaterName" min-width="90" />
      <el-table-column label="最后修改时间" align="center" prop="createTime" min-width="120">
        <template slot-scope="scope">
          <el-popover
            placement="top-start"
            trigger="click"
          >
            <div class="ellipsis-all-content">{{ scope.row.flastupdatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</div>
            <div slot="reference" class="ellipsis">{{ scope.row.flastupdatetime | dateFilter('yyyy-mm-dd hh:mm:ss') }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="120">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改分部分项对话框 -->
    <el-dialog :title="title" :visible.sync="open" :close-on-click-modal="false" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="编码" prop="fnumber">
          <el-input v-model="form.fnumber" placeholder="请输入编码名称" :maxlength="80" />
        </el-form-item>
        <el-form-item label="名称" prop="fnameL2">
          <el-input v-model="form.fnameL2" placeholder="请输入分部分项名称" :maxlength="255" />
        </el-form-item>
        <el-form-item label="专业类别" prop="cfspecialtypeid">
          <el-select v-model="form.cfspecialtypeid" placeholder="请选择" style="width: 380px;">
            <el-option
              v-for="item in specialTypes"
              :key="item.fid"
              :label="item.fnameL2"
              :value="item.fid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="fdescriptionL2">
          <el-input v-model="form.fdescriptionL2" type="textarea" placeholder="请输入内容" :maxlength="255" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckType, delCheckType, addCheckType, updateCheckType, listSpecialType } from '@/api/qualityManage/checkType'
let specialTypes = []
export default {
  name: 'CheckType',
  filters: {
    specialType(id) {
      for (let i = 0; i < specialTypes.length; i++) {
        if (specialTypes[i].fid === id) {
          return specialTypes[i].fnameL2
        }
      }
      return id
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 分部分项表格数据
      checkTypeList: [],
      originalList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        fnumber: undefined,
        fnameL2: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fnameL2: [
          { required: true, message: '分部分项名称不能为空', trigger: 'blur' }
        ],
        fnumber: [
          { required: true, message: '分部分项编码不能为空', trigger: 'blur' }
        ],
        cfspecialtypeid: [
          { required: true, message: '请选择专业类别', trigger: 'change' }
        ]
      },
      specialTypes: [] // 专业类别
    }
  },
  created() {
    this.getSpecialList()
  },
  methods: {
    /** 查询分部分项列表 */
    getList() {
      this.loading = true
      listCheckType(this.queryParams).then(response => {
        this.checkTypeList = response.data
        this.originalList = [...response.data]
        this.loading = false
        this.handleQuery()
      })
    },
    /** 查询专业类别列表 */
    getSpecialList() {
      listSpecialType({}).then(response => {
        this.specialTypes = response.data
        specialTypes = response.data
        this.getList()
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        fid: undefined,
        fnumber: undefined,
        fnameL2: undefined,
        fdescriptionL2: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.getList()
      if (this.queryParams.fnumber) {
        this.checkTypeList = this.originalList.filter(type => type.fnumber && type.fnumber.indexOf(this.queryParams.fnumber) !== -1)
      } else {
        this.checkTypeList = this.originalList
      }
      if (this.queryParams.fnameL2) {
        this.checkTypeList = this.checkTypeList.filter(type => type.fnameL2 && type.fnameL2.indexOf(this.queryParams.fnameL2) !== -1)
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加分部分项'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form = { ...row }
      this.open = true
      this.title = '修改分部分项'
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.fid !== undefined) {
            updateCheckType(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addCheckType(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fname = row.fnameL2
      this.$confirm('是否确认删除分部分项名称为"' + fname + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCheckType(row.fid)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(function() {
      })
    }
  }
}
</script>
